package com.itheima.admin.listener;

import com.itheima.admin.service.WemediaNewsAutoScanSevice;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@Slf4j
public class WemediaNewsAutoScanListener {

    @Autowired
    private WemediaNewsAutoScanSevice wemediaNewsAutoScanSevice;


    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_AUTO_SCAN_TOPIC)
    public void autoScanWemediaNews(ConsumerRecord<String ,String> consumerRecord){
        String topic = consumerRecord.topic();
        String newId = consumerRecord.value();
        int partition = consumerRecord.partition();

        String uuid = UUID.randomUUID().toString(); //在多线程的情况下，一篇文章可能存在重复消费，不方便查找日志，添加一个id标识，便于查找错误信息；
        log.info("开始审核 uuid：{},   id:{},   partition:{},  topic:{}",uuid,newId,partition,topic);


        try {
            wemediaNewsAutoScanSevice.autoScanWemediaNews(newId,uuid);
        } catch (Exception e) {
            log.error("开始审核 uuid：{},   id:{},   partition:{},  topic:{}",uuid,newId,partition,topic, e);
        }


        log.info("审核结束  uuid：{},  id:{},   partition:{},  topic:{}",uuid,newId,partition,topic);


    }
}
